/*
 * Profiler.cpp
 *
 *  Created on: 2009-9-23
 *      Author: bingkai
 */

#include "Profiler.h"
#include "Utility.h"
#include "string.h"
#include "QFile.h"

Profiler::Profiler()
:begin(0), end(0)
{
}

Profiler::~Profiler()
{
	
}

void Profiler::Start()
{
	begin = Utility::GetCurrentTime();
}

void Profiler::Stop()
{
	end = Utility::GetCurrentTime();
}

void Profiler::Print(const char* str)
{
	if(str == NULL)
	return;
	
	CQSString info;
	info.SetData(str);
	Print(info);
}

void Profiler::Print(const CQSString& aInfo)
{
	#ifdef PROFILER_DEBUG


	CQFile file;
	CQWString wStr;
	TTime time(end - begin);
	
	TBuf<20> tBuf;
	time.FormatL(tBuf,_L("%Tm %Ss %*C3ms"));

	TBuf<50> buf;
	time.HomeTime();
	time.FormatL(buf, _L("[%F%Y-%M-%D %H:%T:%S]\t\t"));
	
	wStr.AppendData((LPCWSTR)buf.PtrZ());
    
	CQSString info(wStr);
	info.AppendData((QBYTE*)aInfo.GetDataPtr(), aInfo.GetLength());
	info.AppendData("\t\ttime:");
	
	wStr.Empty();
	wStr.AppendData((LPCWSTR)tBuf.PtrZ());
	CQSString sStr(wStr);
	info.AppendData((QBYTE*)sStr.GetDataPtr(), sStr.GetLength());
	info.AppendData("\r\n\r\n");
	
	if(file.Open((LPCWSTR)L"C:\\data\\profile.txt", CQFile::modeAppend))
	{
		
		file.Write(info.GetDataPtr(), info.GetLength());
		file.Close();
	}
	
#endif
}
